﻿/*
 * @author: wizardc
 */

using System.Collections.Generic;
using UnityEngine;

namespace Dou.UI
{
    /// <summary>
    /// UGUI 富文本资源
    /// </summary>
    public class RichTextAsset : ScriptableObject
    {
        /// <summary>
        /// 文本材质
        /// </summary>
        public Material material;
        
        /// <summary>
        /// 贴图
        /// </summary>
        public Texture2D spriteSheet;
        
        /// <summary>
        /// 精灵尺寸
        /// </summary>
        public float spriteSize;
        
        /// <summary>
        /// 图集中的行数
        /// </summary>
        public int column;
        
        /// <summary>
        /// 精灵对象信息
        /// </summary>
        public List<SpriteData> spriteInfoList;

        private Dictionary<string, SpriteData> _spriteInfoMap;
        
        public bool TryGetSprite(string name, out SpriteData spriteData)
        {
            if (_spriteInfoMap == null)
            {
                _spriteInfoMap = new Dictionary<string, SpriteData>();
                foreach (var info in spriteInfoList)
                {
                    _spriteInfoMap.Add(info.name, info);
                }
            }
            return _spriteInfoMap.TryGetValue(name, out spriteData);
        }
    }

    [System.Serializable]
    public class SpriteData
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string name;
        
        /// <summary>
        /// 精灵的索引
        /// </summary>
        public int index;
        
        /// <summary>
        /// 如果是序列帧动画，则这里是总帧数，否则为 1
        /// </summary>
        public int frameCount;
        
        /// <summary>
        /// 如果是序列帧动画，这里是播放速度
        /// </summary>
        public float speed = 1f;
    }
}
